package com.hspedu.method;

/**
 * @author DL5O
 * @version 1.0
 * 守护线程的设置
 */
public class ThreadMethod03 {
    public static void main(String[] args) throws InterruptedException {
        MyDaemonThread myDaemonThread = new MyDaemonThread();
        Test01 test01 = new Test01();
        myDaemonThread.setDaemon(true);
        //如果我们希望当main线程结束后，子线程可以自动退出
        //只需将子线程设为守护线程即可
        myDaemonThread.start();
        test01.start();

        for (int i = 1; i <= 10; i++) {
            System.out.println("我是主线程 " + i);
            Thread.sleep(1000);
        }

    }
}

class MyDaemonThread extends Thread {
    @Override
    public void run() {
        for (int i = 1; ; i++ ) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("我是MyDaemonThread 守护线程 " + i);
        }
    }
}

class Test01 extends Thread {
    @Override
    public void run() {
        for (int i = 1; i <= 15 ; i++ ) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("我是Test01 用户线程 " + i);
        }
    }
}